﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0656 //2010.10.23 //Begin
namespace InfoJetSoft.Service.Worker
{
    class PNGDeletePrincipalWorker
    {
        public static void deletePrincipal(Form form)
        {
            string contextId = form.ParamTable["xdoc_param_png_context_id"] as string;
            //CID 0914 //2011.05.20 //Begin
            string pngId = form.ParamTable["xdoc_param_png_id"] as string;
            //CID 0914 //2011.05.20 //End
            XmlElement contextElement = DOMUtil.findNodeByJetId(form, contextId) as XmlElement;
            if (contextElement != null)
            {
                string accountId = form.ParamTable["xdoc_param_png_account_id"] as string;

                //CID 0914 //2011.05.20 //Begin
                PNGInfo pngInfo = form.Manifest.PNGInfoTable[pngId] as PNGInfo;
                if (pngInfo.AllowMultiple.Equals("false"))
                {
                    contextElement.InnerXml = "";
                }
                string accountIdFilter = NamespaceUtil.toFullName(pngInfo.Prefix, "Person") + "/" + NamespaceUtil.toFullName(pngInfo.Prefix, "AccountId");
                //CID 0914 //2011.05.20 //End
                ArrayList accoundIdList = new ArrayList();
                XmlNodeList accountIdElementList = DOMUtil.selectNodes(contextElement, accountIdFilter, 0, form.XmlNsResolver);
                foreach (XmlElement accountIdElement in accountIdElementList)
                {
                    string curAccountId = accountIdElement.InnerText.Trim();
                    if (curAccountId.Length <= 0)
                    {
                        //CID 0828 //2011.03.11 //Begin
                        if (form.FocusItemIndex == -1)
                        {
                            form.FocusItemIndex = DOMUtil.getChildElementIndex(contextElement, (XmlElement)accountIdElement.ParentNode);
                        }
                        //CID 0828 //2011.03.11 //End
                        contextElement.RemoveChild(accountIdElement.ParentNode);
                    }
                    else
                    {
                        if (curAccountId.Equals(accountId))
                        {
                            //CID 0828 //2011.03.11 //Begin
                            if (form.FocusItemIndex == -1)
                            {
                                form.FocusItemIndex = DOMUtil.getChildElementIndex(contextElement, (XmlElement)accountIdElement.ParentNode);
                            }
                            //CID 0828 //2011.03.11 //End
                            contextElement.RemoveChild(accountIdElement.ParentNode);
                        }
                    }
                }
            }
        }
    }
}
//CID 0656 //2010.10.23 //End
